package model

import "fmt"

type RedPacket struct {
	Id       int
	UniqueId string
	Level    int
	Amount   int
	Type     int
	Time     int
	Friend   string
	RedCode  string
	DrawTime int
	GetTime  int
}

type DailyDraw struct {
	OpenId string
	Money  int
	Time   int
}

type WithDraw struct {
	OpenId      string
	Amount      int
	Type        int
	OrderNo     string
	ErrCode     string
	ErrDesc     string
	Ip          string
	Time        int
	PaymentTime int
}

func (redPacket *RedPacket) Create() (id int, err error) {
	sql := "INSERT INTO packet (unique_id, level, amount, type, redCode, time, getTime, friend) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
	stmt, err := Db.Prepare(sql)
	if err != nil {
		return
	}
	defer stmt.Close()

	res, err := stmt.Exec(redPacket.UniqueId, redPacket.Level, redPacket.Amount, redPacket.Type, redPacket.RedCode, redPacket.Time, redPacket.GetTime, redPacket.Friend)
	if err != nil {
		fmt.Println(err)
		return
	}
	idx, err := res.LastInsertId()
	if err != nil {
		return
	}
	id = int(idx)
	return
}

func RedByCode(redCode string) (redPacket RedPacket, err error) {
	redPacket = RedPacket{}
	err = Db.QueryRow("SELECT id, unique_id, type FROM packet WHERE redCode = ?", redCode).Scan(&redPacket.Id, &redPacket.UniqueId, &redPacket.Type)
	return
}

func RedByLevel(openId string, level int) (id int, err error) {
	err = Db.QueryRow("SELECT id FROM packet WHERE unique_id = ? and level = ?", openId, level).Scan(&id)
	return
}

func GetRedCount(openId string) (count int, err error) {
	err = Db.QueryRow("SELECT count(id) FROM packet WHERE unique_id = ?", openId).Scan(&count)
	return
}

func GetRedSum(openId string) (sum int, err error) {
	err = Db.QueryRow("SELECT sum(amount) FROM packet WHERE type in(1,2,4) and unique_id = ?", openId).Scan(&sum)
	return
}

func Switch() (flag int, desc string, err error) {
	err = Db.QueryRow("SELECT switch,drawDesc FROM drawSwitch WHERE id = 1").Scan(&flag, &desc)
	return
}

func (withDraw *WithDraw) Create() (id int, err error) {
	sql := "INSERT INTO withdraw (openId, amount, orderNo, type, errCode, errDesc, ip, time, paymentTime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
	stmt, err := Db.Prepare(sql)
	if err != nil {
		return
	}
	defer stmt.Close()

	res, err := stmt.Exec(withDraw.OpenId, withDraw.Amount, withDraw.OrderNo, withDraw.Type, withDraw.ErrCode, withDraw.ErrDesc, withDraw.Ip, withDraw.Time, withDraw.PaymentTime)
	if err != nil {
		fmt.Println(err)
		return
	}
	idx, err := res.LastInsertId()
	if err != nil {
		return
	}
	id = int(idx)
	return
}

func DrawByTime(openid string, time int) (id int, money int, err error) {
	err = Db.QueryRow("SELECT id, money FROM drawDaily WHERE unique_id = ? AND time = ?", openid, time).Scan(&id, &money)
	return
}

func (dailyDraw *DailyDraw) Create() (id int, err error) {
	sql := "INSERT INTO drawDaily (unique_id, money, time) VALUES (?, ?, ?)"
	stmt, err := Db.Prepare(sql)
	if err != nil {
		return
	}
	defer stmt.Close()

	res, err := stmt.Exec(dailyDraw.OpenId, dailyDraw.Money, dailyDraw.Time)
	if err != nil {
		fmt.Println(err)
		return
	}
	idx, err := res.LastInsertId()
	if err != nil {
		return
	}
	id = int(idx)
	return
}

func DrawDailyUpdate(money int, id int) (err error) {
	statement := "UPDATE drawDaily SET money = ? WHERE id = ?"
	stmt, err := Db.Prepare(statement)
	if err != nil {
		return
	}
	defer stmt.Close()

	_, err = stmt.Exec(money, id)
	return
}
